<!doctype html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Kafka 顺序消费保障机制</title>
    <link rel="stylesheet" href="https://cdn.staticfile.org/font-awesome/6.4.0/css/all.min.css">
    <link rel="stylesheet" href="https://cdn.staticfile.org/tailwindcss/2.2.19/tailwind.min.css">
    <link href="https://fonts.googleapis.com/css2?family=Noto+Serif+SC:wght@400;500;600;700&family=Noto+Sans+SC:wght@300;400;500;700&display=swap" rel="stylesheet">
    <script src="https://cdn.jsdelivr.net/npm/mermaid@latest/dist/mermaid.min.js"></script>
    <style>
        body {
            font-family: 'Noto Sans SC', Tahoma, Arial, Roboto, "Droid Sans", "Helvetica Neue", "Droid Sans Fallback", "Heiti SC", "Hiragino Sans GB", Simsun, sans-serif;
            background: linear-gradient(135deg, #f5f7fa 0%, #c3cfe2 100%);
            min-height: 100vh;
        }
        
        .hero-gradient {
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
            position: relative;
            overflow: hidden;
        }
        
        .hero-gradient::before {
            content: '';
            position: absolute;
            top: -50%;
            right: -50%;
            bottom: -50%;
            left: -50%;
            background: radial-gradient(circle, rgba(255,255,255,0.1) 0%, transparent 70%);
            animation: pulse 4s ease-in-out infinite;
        }
        
        @keyframes pulse {
            0%, 100% { transform: scale(1); opacity: 0.5; }
            50% { transform: scale(1.1); opacity: 0.8; }
        }
        
        .content-card {
            background: rgba(255, 255, 255, 0.95);
            backdrop-filter: blur(10px);
            box-shadow: 0 20px 40px rgba(0, 0, 0, 0.1);
            transition: all 0.3s ease;
        }
        
        .content-card:hover {
            transform: translateY(-5px);
            box-shadow: 0 25px 50px rgba(0, 0, 0, 0.15);
        }
        
        .section-title {
            position: relative;
            padding-left: 20px;
        }
        
        .section-title::before {
            content: '';
            position: absolute;
            left: 0;
            top: 50%;
            transform: translateY(-50%);
            width: 4px;
            height: 80%;
            background: linear-gradient(180deg, #667eea 0%, #764ba2 100%);
            border-radius: 2px;
        }
        
        .first-letter {
            float: left;
            font-size: 4em;
            line-height: 0.8;
            margin: 0.1em 0.1em 0 0;
            font-weight: 700;
            color: #667eea;
            font-family: 'Noto Serif SC', serif;
        }
        
        .image-container {
            position: relative;
            overflow: hidden;
            border-radius: 12px;
            box-shadow: 0 10px 30px rgba(0, 0, 0, 0.1);
        }
        
        .image-container img {
            transition: transform 0.3s ease;
        }
        
        .image-container:hover img {
            transform: scale(1.05);
        }
        
        .mermaid-container {
            background: #f8f9fa;
            border-radius: 12px;
            padding: 2rem;
            box-shadow: inset 0 2px 4px rgba(0, 0, 0, 0.06);
        }
        
        .icon-box {
            width: 60px;
            height: 60px;
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
            border-radius: 16px;
            display: flex;
            align-items: center;
            justify-content: center;
            color: white;
            font-size: 24px;
            box-shadow: 0 10px 20px rgba(102, 126, 234, 0.3);
        }
        
        .highlight-text {
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
            -webkit-background-clip: text;
            -webkit-text-fill-color: transparent;
            font-weight: 600;
        }
    </style>
</head>
<body>
    <!-- Hero Section -->
    <div class="hero-gradient text-white py-20 relative">
        <div class="container mx-auto px-6 relative z-10">
            <div class="max-w-4xl mx-auto text-center">
                <h1 class="text-5xl md:text-6xl font-bold mb-6 animate-fade-in">
                    Kafka 顺序消费保障机制
                </h1>
                <p class="text-xl md:text-2xl opacity-90 leading-relaxed">
                    深入解析分布式消息系统中的顺序性保证策略
                </p>
                <div class="mt-8 flex justify-center space-x-4">
                    <div class="bg-white bg-opacity-20 backdrop-blur-sm rounded-full px-6 py-2">
                        <i class="fas fa-layer-group mr-2"></i>分区设计
                    </div>
                    <div class="bg-white bg-opacity-20 backdrop-blur-sm rounded-full px-6 py-2">
                        <i class="fas fa-users mr-2"></i>消费者组
                    </div>
                    <div class="bg-white bg-opacity-20 backdrop-blur-sm rounded-full px-6 py-2">
                        <i class="fas fa-sync mr-2"></i>偏移量管理
                    </div>
                </div>
            </div>
        </div>
    </div>

    <!-- Main Content -->
    <div class="container mx-auto px-6 py-12 max-w-6xl">
        <!-- Introduction Card -->
        <div class="content-card rounded-2xl p-8 mb-12">
            <div class="flex items-start space-x-6">
                <div class="icon-box">
                    <i class="fas fa-info-circle"></i>
                </div>
                <div class="flex-1">
                    <h2 class="text-3xl font-bold mb-4 text-gray-800">核心概述</h2>
                    <p class="text-lg text-gray-700 leading-relaxed">
                        <span class="first-letter">在</span>Kafka中，顺序消费的实现主要依赖于分区和消费者组的设计。通过将相关数据发送到同一个分区，以及每个分区只分配给一个消费者来消费，确保了消息的顺序性。消费者组配置要合理，每个消费者只消费一个分区的数据，从而避免了同一消息被多个消费者处理的情况。
                    </p>
                </div>
            </div>
            
            <div class="mt-8 mermaid-container">
                <div class="mermaid">
                    graph LR
                        A[生产者] -->|有序写入| B[Partition 0]
                        A -->|有序写入| C[Partition 1]
                        A -->|有序写入| D[Partition 2]
                        B -->|顺序消费| E[Consumer 0]
                        C -->|顺序消费| F[Consumer 1]
                        D -->|顺序消费| G[Consumer 2]
                        
                        style A fill:#667eea,stroke:#fff,stroke-width:2px,color:#fff
                        style B fill:#f8f9fa,stroke:#667eea,stroke-width:2px
                        style C fill:#f8f9fa,stroke:#667eea,stroke-width:2px
                        style D fill:#f8f9fa,stroke:#667eea,stroke-width:2px
                        style E fill:#764ba2,stroke:#fff,stroke-width:2px,color:#fff
                        style F fill:#764ba2,stroke:#fff,stroke-width:2px,color:#fff
                        style G fill:#764ba2,stroke:#fff,stroke-width:2px,color:#fff
                </div>
            </div>
        </div>

        <!-- Section 1: 分区设计 -->
        <div class="content-card rounded-2xl p-8 mb-8">
            <h2 class="section-title text-3xl font-bold mb-6 text-gray-800">
                <i class="fas fa-layer-group mr-3 text-purple-600"></i>分区设计
            </h2>
            <p class="text-lg text-gray-700 leading-relaxed mb-6">
                在Kafka中保证顺序消费的关键之一是分区设计。每个主题可以被分为多个分区，而每个分区内的消息是有序的。因此，通过合理的分区设计，将相关的消息发送到同一个分区，可以保证这些消息被消费者按照发送顺序进行处理。
            </p>
            
            <div class="grid md:grid-cols-2 gap-6 mb-6">
                <div class="bg-gradient-to-br from-purple-50 to-pink-50 p-6 rounded-xl">
                    <h3 class="text-xl font-semibold mb-3 text-gray-800">
                        <i class="fas fa-code mr-2 text-purple-600"></i>生产者端策略
                    </h3>
                    <p class="text-gray-700">使用自定义分区器确保相关消息被发送到同一个分区，保证消息的有序性。</p>
                </div>
                <div class="bg-gradient-to-br from-blue-50 to-purple-50 p-6 rounded-xl">
                    <h3 class="text-xl font-semibold mb-3 text-gray-800">
                        <i class="fas fa-download mr-2 text-blue-600"></i>消费者端策略
                    </h3>
                    <p class="text-gray-700">每个消费者只消费一个分区的消息，从而保证了消息的顺序性。</p>
                </div>
            </div>
        </div>

        <!-- Section 2: 分区数与消费者数的关系 -->
        <div class="content-card rounded-2xl p-8 mb-8">
            <h2 class="section-title text-3xl font-bold mb-6 text-gray-800">
                <i class="fas fa-balance-scale mr-3 text-purple-600"></i>分区数与消费者数的关系
            </h2>
            <p class="text-lg text-gray-700 leading-relaxed mb-6">
                为了确保顺序消费，应确保每个分区只分配给一个消费者。因此，当配置消费者组时，应该将消费者数量设置为分区数量的整数倍，以确保每个分区只被一个消费者消费。
            </p>
            
            <div class="bg-gray-50 rounded-xl p-6 mb-6">
                <div class="grid md:grid-cols-3 gap-4">
                    <div class="text-center">
                        <div class="text-4xl font-bold highlight-text mb-2">1:1</div>
                        <p class="text-gray-600">最佳配置</p>
                        <p class="text-sm text-gray-500 mt-2">每个分区对应一个消费者</p>
                    </div>
                    <div class="text-center">
                        <div class="text-4xl font-bold text-orange-500 mb-2">N:1</div>
                        <p class="text-gray-600">可接受配置</p>
                        <p class="text-sm text-gray-500 mt-2">多个分区对应一个消费者</p>
                    </div>
                    <div class="text-center">
                        <div class="text-4xl font-bold text-red-500 mb-2">1:N</div>
                        <p class="text-gray-600">错误配置</p>
                        <p class="text-sm text-gray-500 mt-2">破坏顺序性</p>
                    </div>
                </div>
            </div>
        </div>

        <!-- Section 3: 消费者组配置 -->
        <div class="content-card rounded-2xl p-8 mb-8">
            <h2 class="section-title text-3xl font-bold mb-6 text-gray-800">
                <i class="fas fa-users-cog mr-3 text-purple-600"></i>消费者组配置
            </h2>
            <p class="text-lg text-gray-700 leading-relaxed mb-6">
                消费者组中的每个消费者应该只消费一个分区的消息，以确保消息的顺序性。因此，在配置消费者组时，需要确保消费者数量不超过分区数量，并且每个消费者都被分配订阅主题的某些分区。
            </p>
            
            <div class="mermaid-container">
                <div class="mermaid">
                    graph TB
                        subgraph "Topic: Orders"
                            P0[Partition 0<br/>用户A的订单]
                            P1[Partition 1<br/>用户B的订单]
                            P2[Partition 2<br/>用户C的订单]
                        end
                        
                        subgraph "Consumer Group"
                            C0[Consumer 0]
                            C1[Consumer 1]
                            C2[Consumer 2]
                        end
                        
                        P0 -.->|独占消费| C0
                        P1 -.->|独占消费| C1
                        P2 -.->|独占消费| C2
                        
                        style P0 fill:#e0e7ff,stroke:#667eea,stroke-width:2px
                        style P1 fill:#e0e7ff,stroke:#667eea,stroke-width:2px
                        style P2 fill:#e0e7ff,stroke:#667eea,stroke-width:2px
                        style C0 fill:#ddd6fe,stroke:#764ba2,stroke-width:2px
                        style C1 fill:#ddd6fe,stroke:#764ba2,stroke-width:2px
                        style C2 fill:#ddd6fe,stroke:#764ba